package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Course;
import com.sfzy.entity.CourseRating;
import com.sfzy.service.CourseRatingService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("courseRating")
public class CourseRatingController {
    @Autowired
    private CourseRatingService courseRatingService;

    //增
    @PostMapping
    public R add(@RequestBody CourseRating courseRating) {
        int i = courseRatingService.addCourseRating(courseRating);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }

    //改
    @PutMapping
    public R update(@RequestBody CourseRating courseRating) {
        int i = courseRatingService.updateCourseRating(courseRating);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }
    //删
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") int id){
        int i = courseRatingService.deleteCourseRating(id);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }

    //查id
    @GetMapping("{id}")
    public R get(@PathVariable("id") int id){
        CourseRating courseRating = courseRatingService.getById(id);
        return R.ok(courseRating);
    }
    //分页查
    @GetMapping("page")
    public R getByPage(CourseRating courseRating,
                       @RequestParam(required = false,defaultValue = "1") int pageNum,
                       @RequestParam(required = false,defaultValue = "5") int pageSize){
        PageInfo<CourseRating> courseRatingPage = courseRatingService.getByPage(courseRating, pageNum, pageSize);
        return R.ok(courseRatingPage);   
    }

}
